package com.powerunion.datacollection.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.service.FormManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class FormDesignInfoAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(
			FormDesignInfoAction.class);
	private FormManager formManager = null;

	public FormDesignInfoAction() {
		super();
		this.formManager = new FormManager();
	}

	public void finalize() throws Throwable {
		this.formManager = null;
		super.finalize();
	}

	public ActionForward saveMainTblFormCONT(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String saveStr = request.getParameter("formData");
		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("主表信息:" + saveStr);

			boolean isSuccess = false;
			isSuccess = this.formManager.saveMainTblFormCONT(saveStr); 

			if (isSuccess) {
				response.getWriter().write("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}

			transaction.commit();
		} catch (Exception e) {
			try {
				logger.error("", e);
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
			logger.error("", e);
			response.getWriter().write("{failure:true}");
		}

		return null;
	}

	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String fsetId = request.getParameter("fsetId");
		//String mainTblFormCont = "";
//		String mainTblFormCont = this.formManager.getMainTblFormCONT(fsetId);
//		logger.debug("mainTblFormCont:"+mainTblFormCont);
//		if(StringUtils.isEmpty(mainTblFormCont)) {
//			request.setAttribute("mainTblFormCont", null);
//		}else {
//			request.setAttribute("mainTblFormCont", mainTblFormCont);
//		}
		return mapping.findForward("show.view");
	}
	
	public ActionForward getMainTblFormCONT(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/json;charset=utf-8");
		String fsetId = request.getParameter("formId");
		String mainTblFormCont = "";
		mainTblFormCont = this.formManager.getMainTblFormCONT(fsetId);
		logger.debug("主表报送区表单信息："+mainTblFormCont);
		response.getWriter().write("{success:true,data:'" + mainTblFormCont + "'}");
		return null;
	}


	

}
